import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.swing.JTextField;
import java.awt.Dimension;

public class IPPortSettingPane extends JPanel implements propertyReadWritable {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JTextField jTextFieldIP = null;
	private JLabel jLabel1 = null;
	private JTextField jTextFieldPort = null;
	public  int Port;
	public  int[] Ip = new int[4];
	
	/**
	 * This is the default constructor
	 */
	public IPPortSettingPane() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(12, 70, 61, 17));
		jLabel1.setText("PORT");
		jLabel = new JLabel();
		jLabel.setText("IP");
		jLabel.setBounds(new Rectangle(17, 34, 49, 16));
		this.setSize(278, 123);
		this.setLayout(null);
		this.add(jLabel, null);
		this.add(getJTextFieldIP(), null);
		this.add(jLabel1, null);
		this.add(getJTextFieldPort(), null);
	}

	/**
	 * This method initializes jTextFieldIP	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldIP() {
		if (jTextFieldIP == null) {
			jTextFieldIP = new JTextField();
			jTextFieldIP.setBounds(new Rectangle(90, 30, 141, 25));
		}
		return jTextFieldIP;
	}

	/**
	 * This method initializes jTextFieldPort	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldPort() {
		if (jTextFieldPort == null) {
			jTextFieldPort = new JTextField();
			jTextFieldPort.setBounds(new Rectangle(90, 62, 107, 26));
		}
		return jTextFieldPort;
	}
	public String getIPString(){
		return String.format("%d.%d.%d.%d", Ip[0],Ip[1],Ip[2],Ip[3]);
	}
	public void setIPString(String ip){
		String[] tok = ip.split("[^0-9]");
		if(tok.length==4){
			Ip[0] = (int)Integer.valueOf(tok[0]);
			Ip[1] = (int)Integer.valueOf(tok[1]);
			Ip[2] = (int)Integer.valueOf(tok[2]);
			Ip[3] = (int)Integer.valueOf(tok[3]);
		}
	}
	
	public void UpdateData(boolean set){
		if(set){
			jTextFieldPort.setText(String.valueOf(Port));
			jTextFieldIP.setText(String.format("%d.%d.%d.%d", 
					Ip[0],Ip[1],Ip[2],Ip[3]));
		}
		else{
			Port = Integer.valueOf(jTextFieldPort.getText());
			setIPString(jTextFieldIP.getText());
		}
	}

	@Override
	public void readProperty(Properties prop) {
		if(prop==null) return ;
		try{
			String cp = prop.getProperty("DESTINATION-ADDRESS/IP","127.0.0.1");
			setIPString(cp);
			Port 		= Integer.valueOf(prop.getProperty("DESTINATION-ADDRESS/PORT","2700"));
		}catch(NumberFormatException e){
			setIPString("127.0.0.1");
			Port=2700;
		}
		UpdateData(true);
	}

	@Override
	public void writeProperty(Properties prop) {
		if(prop==null) return ;
		prop.setProperty("DESTINATION-ADDRESS/IP", getIPString());
		prop.setProperty("DESTINATION-ADDRESS/PORT", String.valueOf(Port));
	
	}
}  //  @jve:decl-index=0:visual-constraint="95,25"
